package com.molin.extensionTest.getClassNameModel;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;

public class Demo5 {
    public static void main(String[] args) throws ClassNotFoundException, NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException {
        //1.获取class对象
        Class clazz = Class.forName("com.molin.extensionTest.getClassNameModel.Student");

        //2.获取构造方法对象
        Constructor constructor = clazz.getDeclaredConstructor(String.class);


        //3.临时取消访问检查
        //被private修饰的成员，不能直接使用的。如果用反射强行获取并使用，需要临时取消访问检查
        constructor.setAccessible(true);

        //3.创建对象。
        Student zhangsan = (Student) constructor.newInstance("zhangsan");
        System.out.println(zhangsan);
    }
}
